package com.wys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wys.dto.DishDto;
import com.wys.pojo.Dish;
import com.wys.pojo.DishFlavor;
import com.wys.result.R;
import com.wys.service.DishFlavorService;
import com.wys.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     *  新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> insert(@RequestBody DishDto dishDto){
        dishService.insert(dishDto);
        /* //菜品新增后，清除redis中缓存的菜品信息(全局清理)
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);*/
        //菜品新增后，清除redis中缓存的菜品信息(局部清理)
        String key = "dish_"+dishDto.getCategoryId()+"status_"+dishDto.getStatus();
        redisTemplate.delete(key);
        return R.success("新增菜品成功！");
    }

    /**
     *  分页查询
     * @param page 当前页
     * @param pageSize 页面大小
     * @param name 要查询的菜品名称
     * @return
     */
    @GetMapping("/page")
    public R<Page> getPage(int page,int pageSize,String name){
        Page pageInfo = dishService.getPage(page, pageSize, name);
        //如果查询出来的数据的总页面小于当前页,重新查询
        if(page > pageInfo.getPages()){
            pageInfo = dishService.getPage((int)pageInfo.getPages(),pageSize,name);
        }
        return R.success(pageInfo) ;
    }

    /**
     *  根据Id查询时数据，并在页面进行数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable Long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        if(dishDto != null){
            return R.success(dishDto);
        }
        return R.error("没有该菜品的信息！");
    }

    /**
     *  修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> updateWithFlavor(@RequestBody DishDto dishDto){
        R<String> stringR = dishService.updateWithFlavor(dishDto);
       /* //菜品修改后，清除redis中缓存的菜品信息(全局清理)
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);*/
        //菜品修改后，清除redis中缓存的菜品信息(局部清理)
        String key = "dish_"+dishDto.getCategoryId()+"status_"+dishDto.getStatus();
        redisTemplate.delete(key);
        return stringR;
    }

    /**
     *  新增套餐时查询菜品
     * @param dish
     * @return
     */
    /*@GetMapping("/list")
    public R<List<Dish>> getWithSetmeal(Dish dish){
        return dishService.getWithSetmeal(dish);
    }*/
    @GetMapping("/list")
    public R<List<DishDto>> getWithSetmeal(Dish dish){
        return dishService.getWithSetmeal(dish);
    }

    /**
     *  (批量)删除菜品
     * @param ids 要删除的菜品ID(数组)
     * @return
     */
    @DeleteMapping
    public R<String> deleted(@RequestParam List<Long> ids){
        dishService.deleted(ids);
        return R.success("菜品删除成功！");
    }

    /**
     *  修改菜品状态
     * @param status 要修改的状态
     * @param ids  要修改的菜品ID(数组)
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateWithStatus(@PathVariable Integer status,@RequestParam List<Long> ids){

        dishService.updateWithStatus(status,ids);
        return R.success("菜品状态修改成功！");
    }

}
